function out = responseDistanceGaussFit(haxes, distances2,responses2)
%RESPONSEDISTANCEGAUSSFIT    Create plot of datasets and fits
%   RESPONSEDISTANCEGAUSSFIT(DISTANCES2,RESPONSES2)
%   Creates a plot, similar to the plot in the main curve fitting
%   window, using the data that you provide as input.  You can
%   apply this function to the same data you used with cftool
%   or with different data.  You may want to edit the function to
%   customize the code and this help message.
%
%   Number of datasets:  1
%   Number of fits:  1

 
% Data from dataset "responses2 vs. distances2":
%    X = distances2:
%    Y = responses2:
%    Unweighted
%
% This function was automatically generated

% Set up figure to receive datasets and fits
% f_ = gcf;
% f_ = clf;
% figure(f_);

legh_ = []; legt_ = {};   % handles and text for legend
xlim_ = [Inf -Inf];       % limits of x axis
% ax_ = subplot(1,1,1);
ax_ = haxes;
cla;
set(ax_,'Box','on');
axes(ax_); hold on;

% --- Plot data originally in dataset "responses2 vs. distances2"
distances2 = distances2(:);
responses2 = responses2(:);
h_ = line(distances2,responses2,'Parent',ax_,'Color',[0.333333 0 0.666667],...
     'LineStyle','none', 'LineWidth',1,...
     'Marker','o', 'MarkerSize',6);
xlim_(1) = min(xlim_(1),min(distances2));
xlim_(2) = max(xlim_(2),max(distances2));
legh_(end+1) = h_;
legt_{end+1} = 'responses2 vs. distances2';

% Nudge axis limits beyond data limits
if all(isfinite(xlim_))
   xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_);
   set(ax_,'XLim',xlim_)
end

% --- Create fit "fit 1"
fo_ = fitoptions('method','NonlinearLeastSquares','Lower',[-Inf -Inf 0 ]);
st_ = [5 35.35533905933 35.42453587741 ];
set(fo_,'Startpoint',st_);
ft_ = fittype('gauss1' );

% Fit this model using new data
cf_ = fit(distances2,responses2,ft_ ,fo_);
out = cf_;

% % Or use coefficients from the original fit:
% if 0
%    cv_ = {5.173691799703, 2.513983458942e-015, 56.58994265931};
%    cf_ = cfit(ft_,cv_{:});
% end

% Plot this fit
h_ = plot(cf_,'fit',0.95);
% h_ = plot(cf_,'predfunc',0.95);
% h_ = plot(cf_,'predobs',0.95);
legend off;  % turn off legend from plot method call
set(h_(1),'Color',[1 0 0],...
     'LineStyle','-', 'LineWidth',2,...
     'Marker','none', 'MarkerSize',6);
legh_(end+1) = h_(1);
legt_{end+1} = 'fit 1';

hold off;
hleg = legend(ax_,legh_, legt_);
set(hleg, 'Visible', 'off');